package com.example.big_orange2.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.big_orange2.authorization.manager.TokenManager;
import com.example.big_orange2.common.Result;
import com.example.big_orange2.entity.Staff;
import com.example.big_orange2.entity.code;
import com.example.big_orange2.entity.pagedata_s;
import com.example.big_orange2.entity.rechargeorder;
import com.example.big_orange2.mapper.StaffMapper;
import com.example.big_orange2.mapper.rechargeorderMapper;
import com.example.big_orange2.utils.RSAutils;
import javafx.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class StaffService {

    @Resource
    StaffMapper staffMapper;
    @Autowired
    private TokenManager tokenManager;

    //引用公钥
    @Value("${publicKey}")
    private String publicKey;
    //引用私钥
    @Value("${privateKey}")
    private String privateKey;

    @Autowired
    WeChatService weChatService;

    public Result<?> login(@RequestBody Staff staff) {
        Result<String> result=new Result<>();
        RSAutils rsautils=new RSAutils();
        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staffid", staff.getStaffid());
        queryWrapper.eq("passwords",rsautils.decode(staff.getPasswords(),privateKey) );
        Staff res = staffMapper.selectOne(queryWrapper);
        if (res == null) {
            result.setResult("fail");
            result.setMessage("用户名或密码错误");
        }else{
            String token=tokenManager.getToken(staff);
            result.setData(token);
            result.setData1(res);
            result.setCode("200");
            result.setToken(token);
            result.setMessage("登录验证成功");
            result.setResult("success");
        }

        return  result;
    }
    public Result<?> findPage( Integer page, Integer limit, String s){
        pagedata_s ru=new pagedata_s();
        LambdaQueryWrapper<Staff> wrapper =Wrappers.<Staff>lambdaQuery();
        wrapper.eq(Staff::getPositions,2);
        if(StrUtil.isNotBlank(s)){//判断search不是空
            wrapper.like(com.example.big_orange2.entity.Staff::getStaffid,Integer.parseInt(s));
        }
        Page<Staff> ClientPage=staffMapper.selectPage(new Page<>(page,limit),wrapper);
        ru.setStaffPage(ClientPage);
        ru.setTotal(ClientPage.getTotal());
        return  Result.success(ru);//返回结果

    }
    public Result findU(@RequestBody Staff staff) {
        String staffid = staff.getStaffid().toString();
        String passwords = staff.getPasswords();
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        wrapper.eq("staffid",staffid).eq("passwords",passwords);
        Staff one = staffMapper.selectOne(wrapper);
        if (one != null){
            return Result.success(one);
        }else{
            Staff staff1 = new Staff();
            staff1.setStaffid(0);
            return Result.success(staff1);
        }
    }
    public Result<?> get3rd(@RequestBody code code){
        Result<String> result =new Result<>();
        List<String> list = new ArrayList<>();
        try{

            list = weChatService.get3rdSession(code.getCode(),"2");
            result.setToken(list.get(1));
            result.setResult("success");
        }catch (Exception e){
            result.setResult("fail");
            result.setMessage(e.getMessage());
        }
        LambdaQueryWrapper<Staff> wrapper =Wrappers.<Staff>lambdaQuery();
        wrapper.eq(Staff::getOpenid,list.get(0));
        List<Staff> Loanhandle = staffMapper.selectList(wrapper);//
        System.out.println(Loanhandle);
        if(Loanhandle.size() == 0){
            System.out.println("为空情况，无openid对应用户");
            result.setResult("fail");
            result.setMsg("无该微信用户对应后端用户");
            return result;
        }else{
            System.out.println("非空，且查找用户为："+Loanhandle);
        }
        Result<?> results = result.success(Loanhandle.get(0));
        results.setToken(list.get(1));
        results.setResult("success");

        return  results;
    }

    public Result<?> logind_t(@RequestBody Staff staff) {


        Result<?> result=new Result<>();
        Result<?> results = new Result<>();
        RSAutils rsautils=new RSAutils();
        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staffid", staff.getStaffid());
        queryWrapper.eq("passwords", rsautils.decode(staff.getPasswords(),privateKey) );
        Staff res = staffMapper.selectOne(queryWrapper);
        if (res == null) {
            result.setResult("fail");
            result.setMessage("用户名或密码错误");
        }else{
            String token=tokenManager.getTokenS(staff);
            results = result.success(res);
            results.setToken(token);
            results.setResult("success");
            results.setMessage("验证成功");
        }
        System.out.println(results);
        return  results;

    }
    public Result<?> setStaffOpenid(@RequestBody Staff staff, @PathVariable String code) {
        Result<?> result = new Result<>();
        List<String> list = new ArrayList<>();
        System.out.println(staff);
        System.out.println(code);


        try{
            list = weChatService.get3rdSession(code,"2");
            result.setToken(list.get(1));
            result.setResult("success");
        }catch (Exception e){
            result.setResult("fail");
            result.setMessage(e.getMessage());
        }
        System.out.println(list);

        staff.setOpenid(list.get(0));
        staffMapper.updateById(staff);
        result.setCode("0");
        result.setMsg("修改成功");

        return result;
    }

}
